<?php
class Pager 
{
	const DEFAULT_PAGE_CURRENT = 1;
	const DEFAULT_PAGE_SIZE = 10;

	/* 当前页号 */
	protected $page;
	/* 每页显示条数 */
	protected $size;
	/* 总记录数 */
	protected $total;
	/* 总页数 */
	protected $pages;
	/*偏移量*/
	protected $offset;
	/* 是否有上一页 */
	protected $hasPrev = true;
	/* 是否有下一页 */
	protected $hasNext = true;
	/* 链接地址 */
	protected $action;

	public function __construct($params = array()) 
	{
		if (!isset($params['count'])) {
			return false;
		}

		$this->total = $params['count'];
		
		if(isset($params['size']) && !empty($params['size'])) {
			$this->size = $params['size'];
		}

		if(isset($params['page']) && !empty($params['page'])) {
			$_REQUEST['_ppage'] = $params['page'];
		}

		if(isset($params['action']) && !empty($params['action'])) {
			$this->action = $params['action'];
		}
		else {
			 $this->action = $_SERVER['REQUEST_URI'];
		}

		$this->build();
	}

/*
	public function __construct($count, $size = null, $page = null, $action = null) 
	{
		$this->total = $count;
		
		if (!empty($size)) {
			$_REQUEST['_psize'] = $size;
		}
		
		if (!empty($page)) {
			$_REQUEST['_ppage'] = $page;
		}
		
		if (strlen($action)) {
			$this->action = $action;
		} else {
			$this->action = $_SERVER['REQUEST_URI'];
		}
		
		$this->build();
	}
*/	
	private function build() 
	{
		if (isset($_REQUEST['_psize']) && !empty($_REQUEST['_psize'])) {
			$this->size = $_REQUEST['_psize'];	
		} else if ((int)$this->size <= 0) {
			$this->size = self::DEFAULT_PAGE_SIZE;
		}
		
		if (isset($_REQUEST['_ppage']) && !empty($_REQUEST['_ppage'])) {
			$this->page = $_REQUEST['_ppage'];
		} else {
			$this->page = self::DEFAULT_PAGE_CURRENT;
		}
		
		$this->setPages();
		$this->setOffset();
	}
	
	/**
	 * 计算页数 
	 */
	private function setPages() 
	{
		$this->pages = ceil($this->total / $this->size);
		
		if ($this->pages < 1) {
			$this->pages = 1;
		}
		
		if ($this->page > $this->pages) {
			$this->page = $this->pages;
		}
		
		if ($this->page <= 1) {
			$this->hasPrev = false;
		}
		
		if ($this->page == $this->pages) {
			$this->hasNext = false;
		}
		
	}
	
	/**
	 * 计算偏移量 
	 */
	private function setOffset() 
	{
		$this->offset = ($this->page - 1) * $this->size;
	}

	public function getPages() 
	{
		return $this->pages;
	}
	
	public function getOffset() 
	{
		return $this->offset;
	}
	
	public function getSize() 
	{
		return $this->size;
	}
	
	/**
	 * 返回链接地址 
	 */
	private function href() 
	{
		$href = $this->action;

		$href = preg_replace('/&?_psize=\d*/', '', $href);
		$href = preg_replace('/&?_ppage=\d*/', '', $href);
	
		if (!strstr($href, '?')) {
			$href .= '?';
		} 
		
		if(substr($href, -1) != '?'){
			$href .= '&';
		}

		return $href;
	}
	
	private function setSizeHref($size) 
	{
		$href = $this->href();
		
		$href .= '_psize=' . $size . '&';
		
		return $href;
	}
	
	private function setPageHref($page) 
	{
		$href = $this->href();
		$href .= '_ppage=' . $page . '&';
		
		return $href;
	}
	
	/**
	 * 获取分页html代码
	 * @param bool $show 是否显示跳转页代码
	 */
	public function getHtml($show = false) 
	{
		$href = $this->setSizeHref($this->size);
		
		$html = '<div class="yellow">';
		$html .= "<span class='pages'><label>总数<span class='value'>{$this->total}</span>条 每页<span class='value'>" . $this->getSizeHtml($this->size) . "</span>条  共<span class='value'>{$this->page}</span>/<span class='value'>$this->pages</span>页</label>";
		if ($this->hasPrev) {
			$html .= '<a href="' . $href . '_ppage=1' . '">|&lt</a>';
			$html .= '<a href="' . $href . '_ppage=' . ($this->page - 1) . '">&lt;</a>';
		} else {
			$html .= '<span class="disabled">|&lt;</span>';
			$html .= '<span class="disabled">&lt;</span>';
		}
		
		$html .= '&nbsp';
		
		// 数字分页
		$left = $this->page - 2;
		if ($left < 1) {
			$left = 1;
		}
		
		if ($left + 4 > $this->pages) {
			$left -= $this->pages - $left;
		}
		
		if ($this->pages - $this->page < 3) {
			$left = $this->pages - 4;
		}
		
		if ($left < 1) {
			$left = 1;
		}

		for ($i = $left; $i <= $left + 4; $i++) {
			if ($this->page == $i) {
				$html .= '<span class="current">' . $i . '</span>&nbsp;';
			} else {
				$html .= '<a href="' . $href . '_ppage=' . $i . '">' . $i . '</a>&nbsp;';
			}
			
			if ($i >= $this->pages) {
				break;		
			}	
		}
		
		$r = '';
		if ($left + 4 < $this->pages) {
			for ($i = $this->pages; $i > $this->pages - 2; $i--) {
				if ($i <= $left + 4) {
					break;
				}
				$r =  '<a href="'  . $href . '_ppage=' . $i . '">' . $i . '</a>&nbsp;' . $r;
			}
			
			$html .= $r;
		}
		
		if ($this->hasNext) {
			$html .= '<a href="' . $href . '_ppage=' . ($this->page + 1) . '">&gt;</a>';
			$html .= '<a href="' . $href . '_ppage=' . $this->pages . '">&gt;|</a>';
		} else {
			$html .= '<span class="disabled">&gt;</span>';
			$html .= '<span class="disabled">&gt;|</span>';
		}
		
		if($show){
			$rdeictHtml = $this->createRedirectHtml();
			$html .= $rdeictHtml;
		}
		
		$html .= '</div>';
		return $html;
	}
	
	private function createRedirectHtml()
	{
		$href = $this->setSizeHref($this->size);

		$html = " 跳转到&nbsp;<input style='width:25px;' id='pageredirect' onkeyup='if(event.keyCode == 13){window.location.href=\"{$href}_ppage=\" + this.value;}' />页<a href='#' onclick='var pageredirect = document.getElementById(\"pageredirect\");if(pageredirect.value != \"\"){window.location.href=\"{$href}_ppage=\" + pageredirect.value;}'>GO</a>";
		return $html;
	}
	
	private function getSizeHtml($size)
	{
		$sizeOptions = array(10, 20, 50, 100);
		if (!in_array((int)$size, array_values($sizeOptions))) {
			array_push($sizeOptions, (int)$size);
		}
		
		$href = $this->setSizeHref($size);
	
		$html = "<select id='_psize' name='_psize' onchange='window.location.href=\"{$href}_psize=\" + this.value;'>";
		foreach ($sizeOptions as $val) {
			$optionHtml = "<option value='{$val}' " . ((int)$size === $val ? 'selected' : null) . ">{$val}</option>";
			$html .= $optionHtml;
		}
		$html .= "</select>";

		return $html;
	}
}